// pages/mine/publish/editDetail/editDetail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    picker: ['通知', '志愿', '文体', "竞赛", "讲座", "其他"],
    id: null,
    date: '2018-12-25',
    index: 0,
    url: '',
    title: '',
    content: '',
    source: ''
  },
  formSubmit: function(e) {
    var that = this
    console.log(e.detail.value)
    if (e.detail.value.title.length == 0) {
      wx.showModal({
        content: '标题不得为空！',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    } else if (e.detail.value.source.length == 0) {
      wx.showModal({
        content: '来源不得为空！',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    } else if (e.detail.value.url.length == 0) {
      wx.showModal({
        content: '链接不得为空！',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    } else if (e.detail.value.content.length == 0) {
      wx.showModal({
        content: '摘要不得为空！',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    } else if (e.detail.value.source.length == 0) {
      wx.showModal({
        content: '来源不得为空！',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    } else {
      wx.request({
        url: app.globalData.url + "news/" + that.data.id,
        data: {
          title: e.detail.value.title,
          content: e.detail.value.content,
          source: e.detail.value.source,
          url: e.detail.value.url,
          type: Number(that.data.index) + 1,
          end_time: that.data.date + " 23:59:59",
        },
        method: "PUT",
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          console.log("------上传------")
          console.log(res)
          var data = res.data
          if (data.state == 1) {
            wx.showToast({
              title: "编辑成功", //这里打印出登录成功
              icon: 'success',
              duration: 1000
            })
            setTimeout(function () {
             wx.reLaunch({
                url: '/pages/news/news',
              })
            }, 1000)
          } else {
            wx.showToast({
              title: "未知错误",
              icon: 'none',
              duration: 1000
            })
          }
        }
      })
      
      
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var news = JSON.parse(options.news)
    news.end_time = news.end_time.trim().split(" ")[0]
    this.setData({
      id: news.id,
      date: news.end_time,
      index: news.type - 1,
      url: news.url,
      title: news.title,
      content: news.content,
      source: news.source
    })
    console.log(news.content)

  },
  PickerChange(e) {
    console.log("----PickerChange----")
    console.log(e.detail.value);
    this.setData({
      index: e.detail.value
    })
  },
  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})